package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.NewPayModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Hq on 2017/7/17.
 */
public class NewPayPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mNewPayService;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mNewPayService = Http.getNewPayService(mContext);
    }

    public void getNewPayOrder(String merOrderId, String submitTime, String msgCiphertext, String signValue) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        Call<ResponseData<NewPayModel>> call = mNewPayService.getNewPayOrder("2.0", "EXP01", "11000003177", merOrderId, submitTime, msgCiphertext, "1", signValue, "", "1");
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<NewPayModel>>() {
                         @Override
                         public void onResponse(Call<ResponseData<NewPayModel>> call, Response<ResponseData<NewPayModel>> response) {
                             super.onResponse(call, response);
                             if (response.isSuccessful()) {
                                 ResponseData<NewPayModel> res = response.body();
                                 LogUtils.LogD("新生支付", response.toString() + "------");
//                                 if (res.getRet() == ReturnCode.SUCCESS) {
//                                     mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, mContext.getString(R.string.no_more_oil_address));
//                                     return;
//                                 }
                                 return;
                             }
                             mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                         }

                         @Override
                         public void onFailure(Call<ResponseData<NewPayModel>> call, Throwable t) {
                             super.onFailure(call, t);
                             mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                         }
                     }

        );
    }


    @Override
    public void detachView() {

    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
